var searchRange = function (nums, target) {
  if (nums[0] > target || nums[nums.length - 1] < target) return [-1, -1]
  // let start = -1, end = -1
  // for (var i = 0; i < nums.length; i++) {
  //   if (nums[i] > target) break
  //   if (nums[i] === target) {
  //     start == -1 && (start = i)
  //     start != -1 && (end = i)
  //   }
  // }
  // return [start, end]
  // 利用二分法查找
  let left = 0
  let right = nums.length - 1
  while(left<=right) {
    let mid = Math.floor((left + right)/2)
    if(nums[mid]===target) {
      left = mid
      right = mid
      while(nums[left-1]==target) {
        left--
      }
      while(nums[right+1]===target) {
        right++
      }
      return [left,right]
    }else if(nums[mid]>target) {
      right = mid - 1
    }else{
      left = mid + 1
    }
  }
  return [-1,-1]
};
let nums = [5, 7, 7, 8, 8, 10]
console.log(searchRange(nums, 6));